package com.lijianan.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lijianan.entity.*;
import com.lijianan.mapper.MeetingRoomMapper;
import com.lijianan.utils.DateSplit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MeetingRoomServiceImpl implements MeetingRoomService {

    @Autowired
    private MeetingRoomMapper meetingRoomsMapper;

    @Autowired
    private MeetingRoomEquipmentServiceImpl meetingRoomEquipmentServiceImpl;

    @Autowired
    private MeetingRoomEquipmentServiceImpl getMeetingRoomEquipmentServiceImpl;

    @Override
    public List<MeetingRoom> siftMeetingRoom(Date use_time_start, Date use_time_end
            , Integer attendee_count, List<String> equipmentNames) {
        String startTime = DateSplit.spiltHMS(use_time_start);
        String endTime = DateSplit.spiltHMS(use_time_end);
        String start_time = "08:00:00";
        String end_time = "21:00:00";

        Date date = new Date();
        long timeDifference = use_time_start.getTime() - date.getTime();
        long daysDifference = timeDifference / (24 * 60 * 60 * 1000);
        if (daysDifference > 60) {
            System.out.println("客户超过60天预约！");
            return null;
        }

        if (startTime.compareTo(start_time) < 0 || startTime.compareTo(end_time) > 0) {
            System.out.println("开始时间不在可用范围内！");
            return null;
        }
        if (endTime.compareTo(start_time) < 0 || endTime.compareTo(end_time) > 0) {
            System.out.println("结束时间不在可用范围内！");
            return null;
        }
        if (startTime.compareTo(endTime) >= 0) {
            System.out.println("开始时间不早于结束时间！");
            return null;
        }

        List<MeetingRoom> siftMeetingRooms = new ArrayList<>();
        List<MeetingRoom> meetingRooms = selectAll();
        for (MeetingRoom meetingRoom : meetingRooms) {
            boolean isQualified = false;
            if (meetingRoom.getSeating_capacity() >= attendee_count) {
                List<MultimediaEquipment> multimediaEquipments = meetingRoomEquipmentServiceImpl.selectMultimediaEquipmentsByRoomId(meetingRoom.getRoom_id());
                if (multimediaEquipments.size() >= equipmentNames.size()) {
                    int match = 0;
                    for (String equipmentName : equipmentNames) {
                        for (MultimediaEquipment multimediaEquipment : multimediaEquipments) {
                            if (equipmentName.equals(multimediaEquipment.getName())) {
                                match++;
                                break;
                            }
                        }
                    }
                    if (match == equipmentNames.size()) {
                        isQualified = true;
                    }
                }
            }
            if (isQualified)
                siftMeetingRooms.add(meetingRoom);
        }
        return siftMeetingRooms;
    }

    @Override
    public boolean addMeetingRoom(String name, String type, Integer seating_capacity, Double price_per_hour
            , List<String> equipmentNames) {
        if (selectByName(name) != null) {
            System.out.println("该会议室已经被使用！");
            return false;
        }
        MeetingRoom meetingRoom = new MeetingRoom();
        meetingRoom.setName(name);
        meetingRoom.setType(type);
        meetingRoom.setSeating_capacity(seating_capacity);
        meetingRoom.setPrice_per_hour(price_per_hour);
        int insert = meetingRoomsMapper.insert(meetingRoom);
        if (insert > 0) {
            for (String equipmentName : equipmentNames) {
                if (!meetingRoomEquipmentServiceImpl.updateMeetingRoomEquipment(meetingRoom.getRoom_id(), equipmentName)) {
                    return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }

    @Override
    public boolean updateMeetingRoom(Integer room_id, String name, String type, Integer seating_capacity, Double price_per_hour
            , List<String> equipmentNames) {
        MeetingRoom meetingRoom = selectByRoomId(room_id);
        if (meetingRoom == null) {
            System.out.println("该会议室不存在！");
            return false;
        }
        meetingRoom.setType(type);
        meetingRoom.setName(name);
        meetingRoom.setSeating_capacity(seating_capacity);
        meetingRoom.setPrice_per_hour(price_per_hour);
        int update = meetingRoomsMapper.updateById(meetingRoom);
        if (update > 0) {
            for (String equipmentName : equipmentNames) {
                if (!meetingRoomEquipmentServiceImpl.updateMeetingRoomEquipment(meetingRoom.getRoom_id(), equipmentName)) {
                    return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteMeetingRoom(Integer room_id) {
        if (meetingRoomEquipmentServiceImpl.deleteEquipmentByRoomId(room_id)) {
            return meetingRoomsMapper.deleteById(room_id) != 0;
        }
        return false;
    }

    @Override
    public boolean changeStatusToFree(Integer room_id) {
        MeetingRoom meetingRoom = selectByRoomId(room_id);
        if (meetingRoom == null) {
            System.out.println("该会议室不存在！");
            return false;
        }
        meetingRoom.setCurrent_status("空闲");
        return meetingRoomsMapper.updateById(meetingRoom) != 0;
    }

    @Override
    public boolean changeStatusToLock(Integer room_id) {
        MeetingRoom meetingRoom = selectByRoomId(room_id);
        if (meetingRoom == null) {
            System.out.println("该会议室不存在！");
            return false;
        }
        meetingRoom.setCurrent_status("锁定");
        return meetingRoomsMapper.updateById(meetingRoom) != 0;
    }

    @Override
    public boolean changeStatusToSchedule(Integer room_id) {
        MeetingRoom meetingRoom = selectByRoomId(room_id);
        if (meetingRoom == null) {
            System.out.println("该会议室不存在！");
            return false;
        }
        meetingRoom.setCurrent_status("预定");
        return meetingRoomsMapper.updateById(meetingRoom) != 0;
    }

    @Override
    public boolean changeStatusToUse(Integer room_id) {
        MeetingRoom meetingRoom = selectByRoomId(room_id);
        if (meetingRoom == null) {
            System.out.println("该会议室不存在！");
            return false;
        }
        meetingRoom.setCurrent_status("使用");
        return meetingRoomsMapper.updateById(meetingRoom) != 0;
    }

    @Override
    public boolean changeStatusToRepair(Integer room_id) {
        MeetingRoom meetingRoom = selectByRoomId(room_id);
        if (meetingRoom == null) {
            System.out.println("该会议室不存在！");
            return false;
        }
        meetingRoom.setCurrent_status("维护");
        return meetingRoomsMapper.updateById(meetingRoom) != 0;
    }

    @Override
    public MeetingRoom selectByName(String name) {
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return meetingRoomsMapper.selectOne(queryWrapper);
    }

    @Override
    public MeetingRoom selectByRoomId(Integer room_id) {
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id", room_id);
        return meetingRoomsMapper.selectOne(queryWrapper);
    }

    @Override
    public List<MeetingRoom> selectByStatusForSchedule() {
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("current_status", "预定");
        return meetingRoomsMapper.selectList(queryWrapper);
    }

    @Override
    public List<MeetingRoom> selectByStatusForFree() {
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("current_status", "空闲");
        return meetingRoomsMapper.selectList(queryWrapper);
    }

    @Override
    public List<MeetingRoom> selectByStatusForRepair() {
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("current_status", "维修");
        return meetingRoomsMapper.selectList(queryWrapper);
    }

    @Override
    public List<MeetingRoom> selectAll() {
        return meetingRoomsMapper.selectList(null);
    }
}
